package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Override
    public IPage<AgreementVo> pageAgreementByQuery(IPage<AgreementVo> agreementVoIPage, AgreementQueryVo queryVo) {
        IPage<AgreementVo> iPage = baseMapper.selectPageItem(agreementVoIPage, queryVo);
        return iPage;
    }

    @Override
    public AgreementVo getAgreementById(Long id) {
        // 租约
        LeaseAgreement leaseAgreement = baseMapper.selectById(id);

        // 公寓
        Long apartmentId = leaseAgreement.getApartmentId();
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(apartmentId);

        // 房间
        Long roomId = leaseAgreement.getRoomId();
        RoomInfo roomInfo = roomInfoMapper.selectById(roomId);

        // 支付方式
        Long paymentTypeId = leaseAgreement.getPaymentTypeId();
        PaymentType paymentType = paymentTypeMapper.selectById(paymentTypeId);

        // 租期
        Long leaseTermId = leaseAgreement.getLeaseTermId();
        LeaseTerm leaseTerm = leaseTermMapper.selectById(leaseTermId);

        AgreementVo agreementVo = new AgreementVo();
        BeanUtils.copyProperties(leaseAgreement, agreementVo);
        agreementVo.setApartmentInfo(apartmentInfo);
        agreementVo.setRoomInfo(roomInfo);
        agreementVo.setPaymentType(paymentType);
        agreementVo.setLeaseTerm(leaseTerm);
        return agreementVo;
    }

    @Override
    public void updateStatus() {
        List<LeaseAgreement> leaseAgreements = baseMapper.selectList(null);
        for (LeaseAgreement leaseAgreement : leaseAgreements){
            Date leaseStartDate = leaseAgreement.getLeaseStartDate();
            Date now = new Date();
            if (leaseStartDate.before(now)){
                System.out.println("++++++++++该合同已到期："+leaseAgreement.getId()+"++++++++++");
                leaseAgreement.setStatus(LeaseStatus.EXPIRED);
                baseMapper.updateById(leaseAgreement);
            }

        }
    }
}




